package com.project.weather.utils;


import org.apache.commons.codec.binary.Base64;

import javax.crypto.Cipher;
import java.security.KeyFactory;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;

/**
 * RSA加密与解密
 */
public class RSAEncrypt {
    private static String publicKey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCIVDd9bPf0Xk2RHgyrq4JBAKhfGGanQL3kJe3inv0AbXZmY9PVpdQ9WaTSrDsrBAyhSXoR6wJtUdOBvPw/0W2HCbBjXA0XluP0irI3wZWFVFwcPkv1mQdvvYWKaQPFaokHZBAE0muo92tzbMTHqlUkrRb9gmixtKPEBqtLHNPKXQIDAQAB";
    private static String privateKey = "MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAIhUN31s9/ReTZEeDKurgkEAqF8YZqdAveQl7eKe/QBtdmZj09Wl1D1ZpNKsOysEDKFJehHrAm1R04G8/D/RbYcJsGNcDReW4/SKsjfBlYVUXBw+S/WZB2+9hYppA8VqiQdkEATSa6j3a3NsxMeqVSStFv2CaLG0o8QGq0sc08pdAgMBAAECgYAkhIKUq2LD55rZe3Yjaf6rENUJHA0rUhFLksllAfd9bNThWm489X2jrWaUNCRpQl1rurXjJNNymw+P2iKrqZqvxFFWrLo5FG/+SzlB/4qr9ev+lDhhKO0eQHW/IAx9LEdM/wzPNyPdBrGndoE3ncor2PbgYIfjUastvGQjdpyllQJBAL8qnBDg44e72V/l8tB/3yDnB0j//13XONpEvNYtAlQUxz8AUEqzeEtBYyfJl608MgFoUigHZCEVd6pmAv6YHhcCQQC2kIatMwfoWdZRo2e9NDjOpcQuZ0FT7mKkX1Sry60KwHh1TGUKVgCKlb1+2aU4He0KKvtzOaed34hnYm9KfXerAkBSQRxL6ugcFEjxCXlUG2E/wAiTcYtIrn2I9UCni1BQK2ycAB+MNPLStrzadtRrttXxYxjOnneIALuvtxscvwlbAkBpyS8wtywy17OXVJ8tBFCSdX1LAivZjX19D3xJm9imLUbfeSfnKFRh+Uh3c3gIH1cWGDfh2ZuloDwy49fnCvLHAkEAtjDb7W/kRAajl6+CsnqXYU670Q0Y2ewpOaWyelLgviQ6rvtIcHIdrbWLjqcNR49BfmADv6HFPy19mCpHhdhtDA==";

    /**
     * RSA公钥加密
     *
     *
     * @param str
     *            加密字符串
     * @return 密文
     * @throws Exception
     *             加密过程中的异常信息
     */
    public static String encrypt( String str) throws Exception{
        //base64编码的公钥
        byte[] decoded = Base64.decodeBase64(publicKey);
        RSAPublicKey pubKey = (RSAPublicKey) KeyFactory.getInstance("RSA").generatePublic(new X509EncodedKeySpec(decoded));
        //RSA加密
        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.ENCRYPT_MODE, pubKey);
        String outStr = Base64.encodeBase64String(cipher.doFinal(str.getBytes("UTF-8")));
        return outStr;
    }

    /**
     * RSA私钥解密
     *
     * @param str
     *            加密字符串
     * @return 铭文
     * @throws Exception
     *             解密过程中的异常信息
     */
    public static String decrypt(String str) throws Exception{
        //64位解码加密后的字符串
        byte[] inputByte = Base64.decodeBase64(str.getBytes("UTF-8"));
        //base64编码的私钥
        byte[] decoded = Base64.decodeBase64(privateKey);
        RSAPrivateKey priKey = (RSAPrivateKey) KeyFactory.getInstance("RSA").generatePrivate(new PKCS8EncodedKeySpec(decoded));
        //RSA解密
        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.DECRYPT_MODE, priKey);
        String outStr = new String(cipher.doFinal(inputByte));
        return outStr;
    }

    public static void main(String[] args) throws Exception {
        String encrypt = encrypt("123");
        System.out.println(encrypt);
        String decrypt = decrypt(encrypt);
        System.out.println(decrypt);
    }

}